<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Calibrations_Controller extends CI_Controller {

    function __construct() {
        parent::__construct();

        $this->load->model('result_model', '', true);
        $this->load->model('user', '', true);
        if (!$this->session->userdata('logged_in')) {
            redirect('login', 'refresh');
        }

        $this->load->library('authentication');
        $this->load->helper('t');
        $this->loggedUser = $this->authentication->getLoggedUser();
        $this->authentication->redirectIfNotBe(array('HR', 'SC'));
    }

    function index($region = 'all', $title = 'all', $edit = 'all', $cali = 'all') {

        $this->load->model('quiz_model', '', true);
        $this->load->model('evaluation', '', true);
        $this->load->model('saledetailresult', '', true);
        $this->load->model('attitudedetailresult', '', true);
        $this->load->model('FirstRating', '', true);
        $this->load->model('finalrating', '', true);
        $this->load->model('calibration', '', true);

        $ratings = $this->FirstRating->get_list();
        $ratingDic = array();
        foreach ($ratings as $eachRating) {
            $ratingDic[$eachRating['rating_code']] = $eachRating['name'];
        }

        $evaluation = $this->evaluation->get_last();
        $viewData['evaluation'] = $evaluation;

        if ($evaluation['calibration'] == 'waiting') {
            $this->load->view(get_folder($this->loggedUser->title) . '/calibrations', $viewData);
            return;
        }

        // lấy toàn bộ danh sách nhân viên, trừ ss trở lên
        //$titles = $this->user->get_enum_values();
        $titles = array('NR' => 'NR', 'PG' => 'PG', 'BS' => 'BS', 'Medical' => 'Medical');
        $viewData['titles'] = $titles;

        $this->load->model('region');
        $regions = $this->region->get_list();
        $viewData['regions'] = $regions;

        $regionDic = array();
        foreach ($regions as $key => $value) {
            $regionDic[$value['rsm_code']] = $value['region_name'];
        }
        $viewData['regionDic'] = $regionDic;

        $users = array();
        if ($title != 'all') {
            $viewData['filterTitle'] = $title;
            switch ($title) {
                case 'NR':
                    $users = $this->user->get_list_by_title(array('NR', 'NRTL'), array('status' => 'active'));
                    break;
                case 'PG':
                    $users = $this->user->get_list_by_title(array('PG', 'PGTL'), array('status' => 'active'));
                    break;
                case 'BS':
                    $users = $this->user->get_list_by_title(array('BS', 'BSTL'), array('status' => 'active'));
                    break;
                case 'Medical': $users = $this->user->get_list_by_title(array('MedNR', 'MedBS', 'MedTL'), array('status' => 'active'));
                    break;
            }
        } else {
            $users = $this->user->get_list_by_title(get_employee_titles(), array('status' => 'active'));
        }
        if ($region != 'all') {
            $temp = array();
            foreach ($users as $eachUser) {
                if ($eachUser['rsm_code'] == $region) {
                    $temp[] = $eachUser;
                }
            }
            $users = $temp;
            $viewData['filterRegion'] = $region;
        }

        get_final_results($users, $evaluation['id']);
$asd = 0;$asd2 = 0;
        foreach ($users as &$eachUser) {
            $calibration = $this->calibration->get_by_code_and_eid($eachUser['code'], $evaluation['id']);
            $eachUser['calibration'] = $calibration;

            if ($edit == 'edited') {
                if ($calibration['modify_day'] == null) {
                    unset($users[array_search($eachUser, $users)]);
                    continue;
                }
            } else if ($edit == 'not') {
                if ($calibration['modify_day'] != null) {
                    unset($users[array_search($eachUser, $users)]);
                    continue;
                }
            }

            if ($cali != 'all') {
                if ($calibration['final_result'] != $ratingDic[$cali]) {
                    unset($users[array_search($eachUser, $users)]);
                    continue;
                }
            }
        }

        $viewData['ratings'] = $ratings;

        // hiển thị
        $viewData['rows'] = $users;

        $viewData['filterEdit'] = $edit;
        $viewData['filterCali'] = $cali;

        $this->load->view(get_folder($this->loggedUser->title) . '/calibrations_list', $viewData);
    }

    function calibrate() {
        $this->load->model('evaluation', '', true);
        $this->load->model('calibration');
        $eid = $this->input->post('eid');

        $evaluation = $this->evaluation->get($eid);
        // kiểm tra có phải là last evaluation ko
        //


        if ($evaluation['calibration'] == 'waiting') {
            // copy dữ liệu
            $users = $this->user->get_list_by_title(get_employee_titles(), array('status' => 'active'));
            //$this->get_final_results($users, $eid);
            get_final_results($users, $eid);

            foreach ($users as $eachUser) {

                $newCalibration = array(
                    'user_code' => $eachUser['code'],
                    'eid' => $eid,
                    //'rating_id' => $eachUser['finalRating'] != false ? $eachUser['finalRating']['id'] : '',
                    'final_result' => $eachUser['finalRating'] != false ? $eachUser['finalRating']['name'] : ''
                );
                $this->calibration->insert($newCalibration);
            }

            // cập nhật evaluation thành đã calibrate
            $evaluation['calibration'] = 'calibrated';
            $this->evaluation->update($evaluation);
        }

        //$this->index('all', 'all');
        redirect(get_folder($this->loggedUser->title) . '/calibrations');
    }

    function edit($user_code, $eid) {
        $user = $this->user->get_user_by_code($user_code);
        $this->load->model('calibration');
        $calibration = $this->calibration->get_by_code_and_eid($user_code, $eid);

        if ($this->input->post('Command') == 'Edit') {
            if ($calibration != false) {
                $calibration['final_result'] = $this->input->post('final_rating');
                $calibration['modify_day'] = date('Y-m-d H:i:s');
                $actionResult = $this->calibration->update($calibration);
                $viewData['message'] = $actionResult;
                $viewData['messageType'] = 'success';
            }
        }

        $user['teamleaderInfo'] = $this->user->get_user_by_code($user['teamleader_code']);
        $user['supervisorInfo'] = $this->user->get_user_by_code($user['supervisor_code']);
        $user['rsmInfo'] = $this->user->get_user_by_code($user['rsm_code']);

        $users = array();
        $users[] = $user;

        get_final_results($users, $eid);
        $user = $users[0];

//        if ($calibration != false) {
//            $user['finalRating'] = $calibration['final_result'];
//        }

        $viewData['editUser'] = $user;

        $this->load->model('evaluation');
        $evaluation = $this->evaluation->get($eid);
        $viewData['evaluation'] = $evaluation;

        $ratings = array('', 'Need Improvement', 'Becoming Effective', 'Effective', 'High Effective', 'Exceptional');
        $viewData['ratings'] = $ratings;
        $viewData['calibration'] = $calibration;

        $this->load->view(get_folder($this->loggedUser->title) . '/calibration_edit', $viewData);
    }

}

?>
